﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class FrindLinkDao
    {


        public FrindLinkDao()
        {

            Mapper.CreateMap<E_FrindLink, FrindLinkModel>();
        }
        public List<E_FrindLink> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_FrindLink.Count();

                return edm.E_FrindLink.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public FrindLinkModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_FrindLink.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_FrindLink, FrindLinkModel>(query);
            }

        }
        /// <summary>
        /// 根据网站获取友情连接
        /// </summary>
        /// <param name="stid"></param>
        /// <returns></returns>
        public List<FrindLinkModel> GetFrindLink(int stid) {
            using (ExhibitionEntities edm = new ExhibitionEntities()) {


                if (stid > 0)
                {
                    var query = edm.E_FrindLink.Where(s => s.SiteID == stid);
                    if (query.Count() > 0)
                    {
                        return Mapper.Map<List<E_FrindLink>, List<FrindLinkModel>>(query.ToList());
                    }
                } 
                return Mapper.Map<List<E_FrindLink>, List<FrindLinkModel>>(edm.E_FrindLink.ToList());
                

            
            }
        }



        public int Save(FrindLinkModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_FrindLink.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_FrindLink();

                    edm.AddToE_FrindLink(query);

                }
                query.CreateUserId = obj.CreateUserId;

                query.SiteName = obj.SiteName;
                query.SiteOrderId = obj.SiteOrderId;
                query.SitePic = obj.SitePic;
                query.SiteUrl = obj.SiteUrl;
                query.SiteID = obj.SiteID;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_FrindLink.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

